﻿@page "/System/SysConfigPage"

@namespace SageKing.Studio.Pages.Config

@implements IMenuTab<SysMenu, long>
@implements IReuseTabsPage

@inject IMessageService _message
@inject IConfirmService _confirmService

@using SageKing.Studio.Pages.Config.component
<PageTitle>@TabTitle</PageTitle>

@inject SysConfigService dataService
@inject SysMenuService _menuService

@if (pageData == null)
{
    <Spin size="SpinSize.Large" Tip="Loading..." />
}
else
{
    <Table @ref="table" TItem="SysConfig" DataSource="@pageData.Items"
           RowKey="x=>x.Id" Size="TableSize.Small"
           Loading="tableLoading"
           @bind-SelectedRows="@selectedRows" Resizable Bordered EnableVirtualization
            Class="main-table" ScrollY="@BrowserConsts.ScrollYTable" PageSize="pageData.PageSize">

        <TitleTemplate>
            <Space>
                <SpaceItem>
                    <Search Placeholder="名称" @bind-Value="searchModel.Name" OnSearch="async ()=>{await OnSearch(pageargs);Message(); }" AllowClear="true" />
                </SpaceItem>

                <SpaceItem>
                    <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.Reload" OnClick="()=>ReSet()">重置</Button>
                </SpaceItem>

                <SpaceItem>
                    <Button Type="@ButtonType.Primary" Icon="@IconType.Outline.AppstoreAdd" OnClick="@AddData">新增</Button>
                </SpaceItem>
            </Space>
        </TitleTemplate>
        <ColumnDefinitions>
            <Selection Type="@selectionType"></Selection>
            <PropertyColumn Property="c=>c.Name" Title="名称" Sortable="true" Filterable />
            <PropertyColumn Property="c=>c.Code" Title="编码" Sortable="true" Filterable />
            <PropertyColumn Property="c=>c.Value" Title="属性值" Sortable="true" Filterable />
            <PropertyColumn Property="c=>c.SysFlag" Title="内置参数" Sortable="true" Align="ColumnAlign.Center" Filterable>
            </PropertyColumn>
            <PropertyColumn Property="c=>c.GroupCode" Title="分组编码" Align="ColumnAlign.Center" Sortable="true" Filterable>
            </PropertyColumn>
            <PropertyColumn Property="c=>c.OrderNo" Title="排序" Align="ColumnAlign.Center" Sortable="true" Filterable>
            </PropertyColumn>
            <PropertyColumn Property="c=>c.Remark" Title="备注" Ellipsis />
            <PropertyColumn Property="c=>c.Id" Title="Id" Sortable="true" Filterable />
            <PropertyColumn Title="时间相关" Property="c=>c.CreateUserName" Align="ColumnAlign.Center">
                <Popover ContentTemplate="@baseRender.GetDetails(context)" Title="时间相关">
                    <Button Icon="@IconType.Outline.OrderedList" Shape="@ButtonShape.Circle" />
                </Popover>
            </PropertyColumn>
            <ActionColumn Title="Action" Fixed="ColumnFixPlacement.Right">
                <Space>
                    <SpaceItem>
                        <Tooltip Title="@IconType.Outline.Edit">
                            <Button OnClick="(e)=>EditData(context)" Shape="@ButtonShape.Circle" Icon="@IconType.Outline.Edit" />
                        </Tooltip>
                    </SpaceItem>
                    <SpaceItem>
                        <Popconfirm Title="确认删除?"
                                    OnConfirm="(e) => DeleteData(context)"
                                    OkText="Yes"
                                    CancelText="No">
                            <Button Icon="@IconType.Outline.Delete" Disabled="@(context.SysFlag == YesNoEnum.Y)" Shape="@ButtonShape.Circle" Danger></Button>
                        </Popconfirm>
                    </SpaceItem>
                </Space>
            </ActionColumn>
        </ColumnDefinitions>
        <PaginationTemplate>
            <Pagination ShowTotal="showTotal" Class="@(context.PaginationClass + " my-custom-pagination")"
                        Total="pageData.Total"
                        PageSize="pageData.PageSize"
                        Current="pageData.Page"
                        ShowSizeChanger
                        ShowQuickJumper
                        OnChange="OnSearch" />
        </PaginationTemplate>
    </Table>

    <Modal Title="@modelTitle" Class="modelDefault"
           @bind-Visible="@_visible" Draggable="true" Centered Closable="false"
           OnOk="HandleOk" ConfirmLoading="addloading" OkText="modelOkTitle" CancelText="modelCancelText">

        @FormParentTemplate()
    </Modal>
}



@code {

    //model base btn title
    string modelTitle = "新增";
    string modelOkTitle = "保存";
    string modelCancelText = "取消";

    //add
    bool _visible = false;
    bool addloading = false;
    SysConfig addModel;
    SysConfigEdit edit;

    //add
    RenderFragment FormParentTemplate()
    {
        return@<SysConfigEdit @ref=@edit Title="@modelTitle" model="@addModel"></SysConfigEdit>;
    }

    async Task HandleOk(MouseEventArgs e)
    {
        try
        { 
            addloading = true;
            if (edit != null)
            { 
                var result = await edit.Me.Save();
                if (!result)
                {
                    _visible = true;
                    addloading = false;
                    return;
                }
            }
            await Refresh(false);
            _ = _message.SuccessAsync($"{modelTitle}【{addModel.Name}】成功！");
            _visible = false;
            addloading = false;
            // addEdit?.Reset();
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex, $"{modelTitle}【{addModel.Name}】失败！");
        }
    }

    #region crud

    void AddData()
    {
        addModel = new SysConfig();
        modelTitle = $"#{TabTitle}-:>新增";
        modelOkTitle = "保存";
        _visible = true;
        addloading = false;

    }

    void EditData(SysConfig row)
    {
        addModel = row.Clone<SysConfig>();
        modelTitle = $"#{TabTitle}-:>编辑";
        modelOkTitle = "保存";
        _visible = true;
        addloading = false;
    }
    async Task DeleteData(SysConfig row)
    {
        if (row.Id <= 0)
        {
            return;
        }
        string content = $"确认删除【{row.Name}】";
        var confirmResult = await _confirmService.Show(content + "?", "提示", ConfirmButtons.OKCancel);
        if (confirmResult == ConfirmResult.OK)
        {
            await dataService.Delete(row.Id);
            await Refresh(false);
            _ = _message.SuccessAsync($"删除【{row.Name}】成功", 2);
            addModel = new SysConfig();
            modelTitle = "新增";
            StateHasChanged();
        }
    }
    #endregion

}


@code {

    bool loading;
    bool IsfirstRender = false;

    BaseRenderFragment baseRender;

    //table
    ITable table;
    bool tableLoading;
     SelectionType selectionType = SelectionType.Checkbox;
    bool isOnRowExpand = false;
    RenderFragment<PaginationTotalContext> showTotal = ctx => @<div class="showTotal">Total: <b>@ctx.Total</b></div>;

    //search
    SysConfig searchModel;

    PaginationEventArgs pageargs = new PaginationEventArgs(1, 10);
    PageBase<SysConfig> pageData;

    IEnumerable<SysConfig> selectedRows;

    #region menu
    public IMenuTab<SysMenu, long> MeTab { get { return this; } }

    public IBaseServiceCache<SysMenu, long> MenuService
    { get { return _menuService; } }

    [SupplyParameterFromQuery(Name = "menuid")]
    public long menuid { get; set; }

    [Parameter]
    public string TabTitle { get; set; } = "参数配置";

    [Parameter]
    public string TabIcon { get; set; } = "home";

    public RenderFragment GetPageTitle() =>
    @<div>
        <Icon Type="@TabIcon" /> @TabTitle
    </div>
    ;
    #endregion

    [CascadingParameter]
    public Error? Error { get; set; }

    protected override async Task OnInitializedAsync()
    {
        try
        {
            baseRender = new BaseRenderFragment();

            await InitSelectData();
            await InitSeachMain();
            await Refresh(false);
            await MeTab.InitTabTitle();
            
        }
        catch (Exception ex)
        {
            Error?.ProcessError(ex);
        }

    }
    async Task InitSelectData()
    {
        selectedRows = new List<SysConfig>();

        await Task.CompletedTask;
    }
    async Task InitSeachMain()
    {
        searchModel = new SysConfig();
        await Task.CompletedTask;
    }

    protected override void OnAfterRender(bool firstRender)
    {
        if (firstRender)
        {
            // Subscribe to the OnResized event. This will do work when the browser is resized.
            //listener.OnResized += MeTable.WindowResized;
            IsfirstRender = firstRender;
        }
    }

    void ReSet()
    {
        searchModel = new SysConfig();
    }
    async Task Refresh(bool notice = true)
    {
        await OnSearch(pageargs);
        if (notice)
        {
            _ = _message.SuccessAsync("刷新成功!");
        }
    }

    public async Task OnSearch(PaginationEventArgs args)
    {
        tableLoading = true;
        pageargs.Page = args.Page;
        pageargs.PageSize = args.PageSize;

        var page = new PageBaseInput() { Page = pageargs.Page, PageSize = pageargs.PageSize };
        Expression<Func<SysConfig, object>> orderbyfunc = a => a.Id;

        List<(bool, Expression<Func<SysConfig, bool>>)> whereIf = new List<(bool, Expression<Func<SysConfig, bool>>)>();

        Expression<Func<SysConfig, bool>> whereIffunc1 = a => a.Name.Contains(searchModel.Name);

        whereIf.Add((searchModel.Name.IsNotNullOrWhiteSpace(), whereIffunc1));

        pageData = await dataService.GetPage(page, whereIf, orderbyfunc, false);

        tableLoading = false;
    }


    void Message(string msg = "查询成功")
    {
        _ = _message.SuccessAsync(msg);
    }

}
